﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace Payroll
{
    class myFile
    {
        public static void myWrite_operate(string name, string operate)//用户操作写入txt
        {
            FileStream fs = new FileStream(@"../../txtFolder/operate.txt", FileMode.Append);//记录用户的操作
            StreamWriter sw = new StreamWriter(fs);
            string timenow = DateTime.Now.ToString();
            
            if (operate == "登录") sw.WriteLine("///");

            sw.WriteLine(timenow + "  |  用户：" + name + "  |  操作：" + operate);

            if (operate == "退出")
            {
                sw.WriteLine("///");
                sw.WriteLine("");
            }
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
            Console.WriteLine("写入成功");
        }
        public static void myDownload(string path)//工资记录写入txt
        {
            FileStream fs = new FileStream(path, FileMode.Create);//记录用户的操作
            StreamWriter sw = new StreamWriter(fs);

            sw.Write("  ");
            sw.Write(String.Format("{0, -5}", "id"));
            sw.Write(String.Format("{0, -8}", "支付金额"));
            sw.Write(String.Format("{0, -12}", "支付时间"));
            sw.Write(String.Format("{0, -12}", "支付方式"));
            sw.WriteLine("");
            sw.WriteLine("———————————————————");

            string sqlStr;
            string connString = @"Data Source=..\..\Database1.sdf";
            SqlCeConnection connection = new SqlCeConnection(connString);
            connection.Open();
            sqlStr = "select * from payemployee order by payDate";
            SqlCeCommand cmd = new SqlCeCommand(sqlStr, connection);
            SqlCeDataReader result = cmd.ExecuteReader();
            
            while(result.Read())
            {
                string id = Convert.ToString(result[0]);
                string pay = Convert.ToString(result[1]);
                DateTime time = Convert.ToDateTime(result[2]);
                string time2 = time.ToString("yyyy-MM-dd");
                string methods = Convert.ToString(result[3]);

                sw.Write("  ");
                sw.Write(String.Format("{0, -5}", id));
                sw.Write(String.Format("{0, -8}", pay));
                sw.Write(String.Format("{0, -12}", time2));
                sw.Write(String.Format("{0, -12}", methods));
                sw.WriteLine("");
            }

            connection.Close();
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        public static void delete(string filePath)
        {
            File.Delete(filePath);
        }
    }
}
